home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_03 / saks / shape2.h < prev   
C/C++ Source or Header  |  1994-01-12  |  3KB  |  163 lines

  1. Listing 4 - the shape class hierarchy with virtual cloning functions 
  2. employing the relaxed return type rules
  3.  
  4. #include <iostream.h>
  5. #include <math.h>
  6. #include <stddef.h>
  7.  
  8. //
  9. // base class 'shape'
  10. //
  11. class shape
  12.     {
  13. public:
  14.     enum palette { BLUE, GREEN, RED };
  15.     shape(palette c);
  16.     virtual double area() const = 0;
  17.     virtual shape *clone() const = 0;
  18.     palette color() const;
  19.     virtual const char *name() const = 0;
  20.     virtual ostream &put(ostream &os) const;
  21. private:
  22.     palette _color;
  23.     static const char *color_image[RED - BLUE + 1];
  24.     };
  25.  
  26. const char *shape::color_image[shape::RED - shape::BLUE + 1] =
  27.     { "blue", "green", "red" };
  28.  
  29. shape::shape(palette c) : _color(c) { }
  30.  
  31. ostream &shape::put(ostream &os) const
  32.     {
  33.     return os << color_image[color()] << ' ' << name();
  34.     }
  35.  
  36. shape::palette shape::color() const
  37.     {
  38.     return _color;
  39.     }
  40.  
  41. //
  42. // class 'circle' derived from 'shape'
  43. //
  44. const double pi = 3.1415926;
  45.  
  46. class circle : public shape
  47.     {
  48. public:
  49.     circle(palette c, double r);
  50.     double area() const;
  51.     circle *clone() const;
  52.     const char *name() const;
  53.     ostream &put(ostream &os) const;
  54. private:
  55.     double radius;
  56.     };
  57.  
  58. circle::circle(palette c, double r) : shape(c), radius(r) { }
  59.  
  60. double circle::area() const
  61.     {
  62.     return pi * radius * radius;
  63.     }
  64.  
  65. const char *circle::name() const
  66.     {
  67.     return "circle";
  68.     }
  69.  
  70. ostream &circle::put(ostream &os) const
  71.     {
  72.     return shape::put(os) << " with radius = " << radius;
  73.     }
  74.  
  75. circle *circle::clone() const
  76.     {
  77.     return new circle(*this);
  78.     }
  79.  
  80. //
  81. // class 'rectangle' derived from 'shape'
  82. //
  83. class rectangle : public shape
  84.     {
  85. public:
  86.     rectangle(palette c, double h, double w);
  87.     double area() const;
  88.     rectangle *clone() const;
  89.     const char *name() const;
  90.     ostream &put(ostream &os) const;
  91. private:
  92.     double height, width;
  93.     };
  94.  
  95. rectangle::rectangle(palette c, double h, double w)
  96.     : shape(c), height(h), width(w) { }
  97.  
  98. double rectangle::area() const
  99.     {
  100.     return height * width;
  101.     }
  102.  
  103. const char *rectangle::name() const
  104.     {
  105.     return "rectangle";
  106.     }
  107.  
  108. ostream &rectangle::put(ostream &os) const
  109.     {
  110.     return shape::put(os) << " with height = " << height
  111.         << " and width = " << width;
  112.     }
  113.  
  114. rectangle *rectangle::clone() const
  115.     {
  116.     return new rectangle(*this);
  117.     }
  118.  
  119. //
  120. // class 'triangle' derived from 'shape'
  121. //
  122. class triangle : public shape
  123.     {
  124. public:
  125.     triangle(palette c, double s1, double s2, double a);
  126.     double area() const;
  127.     triangle *clone() const;
  128.     const char *name() const;
  129.     ostream &put(ostream &os) const;
  130. private:
  131.     double side1, side2, angle;
  132.     };
  133.  
  134. triangle::triangle(palette c, double s1, double s2, double a)
  135.         : shape(c), side1(s1), side2(s2), angle(a) { }
  136.  
  137. double triangle::area() const
  138.     {
  139.     return side1 * sin(angle) * side2 / 2;
  140.     };
  141.  
  142. const char *triangle::name() const
  143.     {
  144.     return "triangle";
  145.     }
  146.  
  147. ostream &triangle::put(ostream &os) const
  148.     {
  149.     return shape::put(os) << " with one side = " << side1
  150.         << ", another side = " << side2
  151.         << " and angle = " << angle;
  152.     }
  153.  
  154. triangle *triangle::clone() const
  155.     {
  156.     return new triangle(*this);
  157.     }
  158.  
  159. ostream &operator<<(ostream &os, const shape &s)
  160.     {
  161.     return s.put(os);
  162.     }
  163.